package com.yuma.tingshu.account.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuma.tingshu.account.service.UserAccountService;
import com.yuma.tingshu.common.annotation.YuMaLogin;
import com.yuma.tingshu.common.constant.SystemConstant;
import com.yuma.tingshu.common.execption.YumaException;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.common.result.ResultCodeEnum;
import com.yuma.tingshu.common.util.AuthContextHolder;
import com.yuma.tingshu.model.account.UserAccount;
import com.yuma.tingshu.model.account.UserAccountDetail;
import com.yuma.tingshu.vo.account.AccountLockResultVo;
import com.yuma.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	@PostMapping("saveAccount/{userId}")
	public Result saveAccount(@PathVariable Long userId){
		this.userAccountService.saveAccount(userId);
		return Result.ok();
	}

	@PostMapping("init/{userId}")
	public Result init(@PathVariable Long userId) {
		this.userAccountService.initAccount(userId);
		return Result.ok();
	}

	@Operation(summary = "查询账户余额")
	@YuMaLogin
	@GetMapping("getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount(){
		Long userId = AuthContextHolder.getUserId();
		UserAccount userAccount = this.userAccountService.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId));
		if (userAccount != null) {
			return Result.ok(userAccount.getAvailableAmount());
		}
		throw new YumaException(ResultCodeEnum.ACCOUNT_ERROR);
	}

	@Operation(summary = "验余额并锁账户")
	@YuMaLogin
	@PostMapping("check/lock")
	public Result<AccountLockResultVo> checkLock(@RequestBody AccountLockVo accountLockVo) {
		AccountLockResultVo resultVo = this.userAccountService.checkLock(accountLockVo);
		return Result.ok(resultVo);
	}

	/**
	 * 查询用户充值记录
	 * @param page
	 * @param limit
	 * @return
	 */
	@YuMaLogin
	@GetMapping("findUserRechargePage/{page}/{limit}")
	public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Integer page, @PathVariable Integer limit) {
		Page<UserAccountDetail> rechargeInfoPage =
				this.userAccountService.findUserRechargePage(page, limit);
		return Result.ok(rechargeInfoPage);
	}

	/**
	 * 查询用户消费记录
	 * @param page
	 * @param limit
	 * @return
	 */
	@YuMaLogin
	@GetMapping("findUserConsumePage/{page}/{limit}")
	public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable Integer page, @PathVariable Integer limit) {
		Page<UserAccountDetail> userAccountDetailPage =
				this.userAccountService.findAccountDetails(SystemConstant.ACCOUNT_TRADE_TYPE_MINUS, page, limit);
		return Result.ok(userAccountDetailPage);
	}
}

